package com.aoxin.util.ageUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ChildrenAgeUtil {
    static Logger logger=LoggerFactory.getLogger(ChildrenAgeUtil.class);
    /**
     * 计算孩子年龄
     * @param fromDate
     * @return
     */
  /*  public static String getAge(Date fromDate) {
        Calendar cal = Calendar.getInstance();
        Date toDate = cal.getTime();

        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH) + 1;
        int fromDay = from.get(Calendar.DAY_OF_MONTH);
        System.out.println("children:" + fromYear + "-" + fromMonth + "-" + fromDay);
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH) + 1;
        int toDay = to.get(Calendar.DAY_OF_MONTH);
        System.out.println("now:" + toYear + "-" + toMonth + "-" + toDay);
        int year = toYear - fromYear;
        int month = toMonth - fromMonth;
        int day = toDay - fromDay;
        if (fromYear == toYear && fromMonth == toMonth) {
            day = toDay - fromDay;
            if (day < 0) {
                System.out.println("未出生的孩子");
            }
        } else if (fromYear == toYear) {
            month = toMonth - fromMonth;
            if (month < 0) {
                System.out.println("未出生的孩子");
            } else {
                day = toDay - fromDay;
                if (day >= 0) {

                } else {
                    month--;
                    int daysOfMonth = getDaysOfMonth(to.getTime());
                    day = daysOfMonth + toDay - fromDay;
                }
            }
        } else if (fromYear != toYear) {
            if (fromYear > toYear) {
                System.out.println("未出生的孩子");
            } else if (fromYear < toYear) {
                year = toYear - fromYear;
                day = toDay - fromDay;
                if (toDay >= fromDay) {
                    if (toMonth >= fromMonth) {
                        month = toMonth-fromMonth;
                    } else {
                        year--;
                        month = 12 + toMonth - fromMonth;
                    }
                } else {
                    int daysOfMonth = getDaysOfMonth(to.getTime());
                    day = daysOfMonth + toDay - fromDay;
                    if (fromMonth == toMonth) {
                        month = 12 + toMonth - fromMonth - 1;
                        year--;
                    } else if (fromMonth > toMonth) {
                        month = fromMonth - toMonth - 1;
                    } else {
                        month = 12 + toMonth - fromMonth - 1;
                        year--;
                    }
                }
            } else {
                System.out.println("未想到的判断");
            }
        }
        System.out.println(year + "-" + month + "-" + day);
        String age =year + "岁" + month + "月" + day+"天";
        if(year<0){
            logger.info("孩子年计算错误");
            age="孩子年计算错误";
        }
        if(month<0){
            logger.info("孩子月计算错误");
            age="孩子月计算错误";
        }
        if(day<0){
            logger.info("孩子日计算错误");
            age="孩子日计算错误";
        }
        return age;
    }*/

    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static String getAge(Date fromDate) {
    	String birth=new DateTime(fromDate).toString("yyyy-MM-dd");
        if (birth == null || "".equals(birth)) {
            return "";
        }
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
        //时间解析
        LocalDate birthday = DateTime.parse(birth, format).toLocalDate();
        LocalDate now = new LocalDate();
        Period period = new Period(birthday, now, PeriodType.yearMonthDay());
        System.out.println(period.getYears()+"岁"+period.getMonths()+"月"+period.getDays()+"天");
        return period.getYears()+"岁"+period.getMonths()+"月"+period.getDays()+"天";
    }

    
    
    public static void main(String[] args) {
       /*  String str=  getAgeFromBirth("2014-06-09");
         System.out.println(str);*/
    }
}
